package com.live.dengdengtv.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.MainHallActivity;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.SimpleInfoMap;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.utils.Notice;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class LoginWayActivity extends BaseActivity implements View.OnClickListener{

    private UMShareAPI mShareAPI;
    private String open_id;
    private String access_token;//
    private String account_type;
    private String image_url = "";
    private String nickname = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_way);

        findViewById(R.id.sina_iv).setOnClickListener(this);
        findViewById(R.id.weixin_iv).setOnClickListener(this);
        findViewById(R.id.qq_iv).setOnClickListener(this);
        findViewById(R.id.phone_iv).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.phone_iv){
            Intent intent = new Intent(context, LoginActivity.class);
            startActivity(intent);
        }else {
            getAuth(v.getId());
        }
    }

    private void getAuth(int id) {

        mShareAPI = UMShareAPI.get( this );

        SHARE_MEDIA platform = null;
        if(id == R.id.sina_iv){
            platform = SHARE_MEDIA.SINA;
            account_type = "2";
        }else if(id == R.id.weixin_iv){
            platform = SHARE_MEDIA.WEIXIN;
            account_type = "3";
        }else if(id == R.id.qq_iv){
            platform = SHARE_MEDIA.QQ;
            account_type = "1";
        }
        /**begin invoke umeng api**/
        mShareAPI.doOauthVerify(this, platform, umAuthListener);
    }

    /** auth callback interface**/
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            if(data == null) return;
            Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
            Log.e("useInfo","user info:"+data.toString());


            if(platform == SHARE_MEDIA.SINA){
                open_id = data.get("uid");
                access_token = data.get("access_token");
                Log.e("SHARE_MEDIA.SINA", open_id);
            }else if(platform == SHARE_MEDIA.WEIXIN){
                open_id = data.get("openid");
                access_token = data.get("access_token");
                Log.e("SHARE_MEDIA.WEIXIN", open_id);
            }else if(platform == SHARE_MEDIA.QQ){
                open_id = data.get("openid");
                access_token = data.get("access_token");
                Log.e("SHARE_MEDIA.QQ", open_id);
            }


            if(TextUtils.isEmpty(open_id)){
                mShareAPI.getPlatformInfo(context, platform, umPlatformAuthListener);
            }else {
                Notice.alert(context, "未获取授权!");
            }
        }
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Log.e("useInfo","未获取授权!");
            Toast.makeText( getApplicationContext(), "未获取授权!", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Log.e("useInfo","未获取授权!");
            Toast.makeText( getApplicationContext(), "未获取授权!", Toast.LENGTH_SHORT).show();
        }
    };

    //获取用户信息
    private UMAuthListener umPlatformAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {

            if (data!=null){
                Toast.makeText(getApplicationContext(), data.toString(), Toast.LENGTH_SHORT).show();
                Log.e("useInfo",data.toString());

                String account = access_token;//TODO: 暂时这么写

                if(platform == SHARE_MEDIA.SINA){
                    nickname = data.get("screen_name");
                    image_url = data.get("profile_image_url");
                }else if(platform == SHARE_MEDIA.WEIXIN){
                    nickname = data.get("screen_name");
                    image_url = data.get("profile_image_url");
                    Log.e("SHARE_MEDIA.WEIXIN", open_id);
                }else if(platform == SHARE_MEDIA.QQ){
                    nickname = data.get("screen_name");
                    image_url = data.get("profile_image_url");
                    Log.e("SHARE_MEDIA.QQ", open_id);
                }


                HashMap<String, String> param = new HashMap<String, String>();
                AppInfo.writeInHeadInfo(param);
                param.put("open_id", open_id);
                param.put("account_type", account_type);
                param.put("account", account);
                param.put("image_url", image_url);
                param.put("nickname", nickname);
//                NetTaskList.getInstance().loginOpenTask(new Gson().toJson(param), loginOpenCallback);
                requestData(NetTaskList.get_bannerList, param,1);

            }
        }
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "get fail", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "get cancel", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if(id == 1){
            AppInfo.loginInfo.setUserId(response.data.getAsJsonObject().get("user_id").toString());
            AppInfo.loginInfo.setAvatarURL(image_url);
            AppInfo.loginInfo.setUserNickname(nickname);
            jumpNext();
        }
        return false;
    }


    private BaseInfoMapCallback loginOpenCallback = new BaseInfoMapCallback(){
        @Override
        public void onError(Call call, Exception e, int id) {
        }
        @Override
        public void onResponse(SimpleInfoMap response, int id) {
            if(response.result == 0){
//                Notice.alert(context, "登陆成功!");
                AppInfo.loginInfo.setUserId(response.data.get("user_id"));
                AppInfo.loginInfo.setAvatarURL(image_url);
                AppInfo.loginInfo.setUserNickname(nickname);
                jumpNext();
            }
        }
    };
    private void jumpNext() {
        Intent intent = new Intent(context, MainHallActivity.class);
        startActivity(intent);
        finish();
    }
}
